import { app, BrowserWindow } from 'electron'
import { session } from "electron";
import '../renderer/store'
import path from 'path' // new
const { exec } = require('child_process');
const { ipcMain } = require('electron');

function setWallpaper(imagePath) {
  // 根据操作系统类型选择设置壁纸的命令
  const isWindows = process.platform === 'win32';
  const isMacOS = process.platform === 'darwin';
  const isLinux = process.platform === 'linux';

  let command;
  if (isWindows) {
    // Windows系统，使用powershell设置壁纸
    command = `powershell.exe -command "Set-ItemProperty -path 'HKCU:\\Control Panel\\Desktop' -name Wallpaper -value '${imagePath}'; rundll32.exe user32.dll, UpdatePerUserSystemParameters"`;
  } else if (isMacOS) {
    // macOS系统，使用osascript设置壁纸
    command = `osascript -e 'tell application "Finder" to set desktop picture to POSIX file "${imagePath}"'`;
  } else if (isLinux) {
    // Linux系统，使用gsettings设置壁纸
    command = `gsettings set org.gnome.desktop.background picture-uri file://${imagePath}`;
  } else {
    console.error('不支持的操作系统');
    return;
  }

    // 执行命令
    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.error('设置壁纸失败:', error);
      } else {
        console.log('壁纸设置成功');
      }
    });

}

ipcMain.on('set-wallpaper', (event, imagePath) => {
  setWallpaper(imagePath);
  console.log('主进程执行啦')
});

// const imagePath = 'C:/Users/52688/Desktop/test.JPG'; // 替换为实际的图片路径
// setWallpaper(imagePath);

app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');
app.commandLine.appendSwitch('disable-site-isolation-trials');


/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

require('electron').app.on('ready', () => {
  // let installExtension = require('electron-devtools-installer')
  // installExtension.default(installExtension.VUEJS_DEVTOOLS)
  //   .then(() => {})
  //   .catch(err => {
  //     console.log('Unable to install `vue-devtools`: \n', err)
  //   })
  BrowserWindow.addDevToolsExtension(path.resolve(__dirname, '../../VueDevtools_6.5.0_0_Chorme')) // new
})

ipcMain.on('set-wallpaper', (event, imagePath) => {
  setWallpaper(imagePath);
  console.log('主进程执行啦')
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
